<!DOCTYPE html>
<html>
<head>
    <title>Physics Diagram - Block and Pulley System</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        canvas {
            border: 1px solid #ccc;
            background-color: #fff;
        }
    </style>
</head>
<body>
    <canvas id="physicsCanvas"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // Canvas dimensions
        const canvasWidth = 500;
        const canvasHeight = 420;
        canvas.width = canvasWidth;
        canvas.height = canvasHeight;

        // --- Drawing Styles ---
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 3; // For a hand-drawn look
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        // --- Dimensions and Positions ---
        // Ground
        const groundY = 350;
        const groundStartX = 40;
        const groundEndX = 480;

        // Block M
        const M_w = 240;
        const M_h = 150;
        const M_x = 100;
        const M_y = groundY - M_h;

        // Block m1
        const m1_w = 90;
        const m1_h = 60;
        const m1_x = M_x + 30;
        const m1_y = M_y - m1_h;

        // Pulley
        const pulley_r = 14;
        const pulley_axle_x = M_x + M_w + 25;
        const pulley_axle_y = M_y - 25;

        // Block m2
        const m2_w = 70;
        const m2_h = 80;
        const m2_y = M_y + 30; // Positioned vertically next to M
        const m2_x = M_x + M_w + 5; // Small gap from M

        // Force F
        const forceArrowY = M_y + M_h / 2;
        const forceArrowStartX = 30;
        const forceArrowEndX = M_x;

        // --- Drawing Functions ---

        // Helper function to draw an arrowhead
        function drawArrowhead(ctx, toX, toY, size) {
            ctx.beginPath();
            ctx.moveTo(toX, toY);
            ctx.lineTo(toX - size, toY - size / 2);
            ctx.moveTo(toX, toY);
            ctx.lineTo(toX - size, toY + size / 2);
            ctx.stroke();
        }

        // --- Draw Scene ---

        // 1. Ground
        ctx.beginPath();
        ctx.moveTo(groundStartX, groundY);
        ctx.lineTo(groundEndX, groundY);
        ctx.stroke();
        // Ground hatching
        const hatchHeight = 12;
        const hatchSpacing = 15;
        for (let x = groundStartX + hatchSpacing; x < groundEndX; x += hatchSpacing) {
            ctx.beginPath();
            ctx.moveTo(x, groundY);
            ctx.lineTo(x - hatchHeight, groundY + hatchHeight);
            ctx.stroke();
        }

        // 2. Block M
        ctx.strokeRect(M_x, M_y, M_w, M_h);
        ctx.font = 'italic 30px Georgia';
        ctx.fillText('M', M_x + M_w / 2, M_y + M_h / 2);

        // 3. Block m1
        ctx.strokeRect(m1_x, m1_y, m1_w, m1_h);
        ctx.font = 'italic 24px Georgia';
        ctx.fillText('m₁', m1_x + m1_w / 2, m1_y + m1_h / 2);

        // 4. Pulley Bracket
        ctx.beginPath();
        ctx.moveTo(M_x + M_w, M_y - 2);
        ctx.lineTo(pulley_axle_x, pulley_axle_y);
        ctx.lineTo(M_x + M_w, pulley_axle_y);
        ctx.stroke();

        // 5. Rope
        const rope_y_horiz = pulley_axle_y - pulley_r;
        const rope_x_vert = pulley_axle_x + pulley_r;
        ctx.beginPath();
        ctx.moveTo(m1_x + m1_w, rope_y_horiz); // Start from right edge of m1
        ctx.lineTo(pulley_axle_x, rope_y_horiz); // Horizontal segment
        ctx.arc(pulley_axle_x, pulley_axle_y, pulley_r, -Math.PI / 2, 0); // Arc around pulley
        ctx.lineTo(rope_x_vert, m2_y + m2_h/2); // Vertical segment down to m2 center
        ctx.stroke();

        // 6. Block m2
        // Position m2 so rope connects to its center
        const m2_final_x = rope_x_vert - m2_w/2;
        ctx.strokeRect(m2_final_x, m2_y, m2_w, m2_h);
        ctx.font = 'italic 24px Georgia';
        ctx.fillText('m₂', m2_final_x + m2_w / 2, m2_y + m2_h / 2);

        // 7. Pulley Wheel (drawn on top of rope and bracket)
        ctx.beginPath();
        ctx.arc(pulley_axle_x, pulley_axle_y, pulley_r, 0, 2 * Math.PI);
        ctx.fillStyle = 'white'; // Fill to hide objects behind it
        ctx.fill();
        ctx.stroke();
        // Pulley axle pin
        ctx.beginPath();
        ctx.arc(pulley_axle_x, pulley_axle_y, 3, 0, 2 * Math.PI);
        ctx.fillStyle = 'black';
        ctx.fill();

        // 8. Force F
        ctx.beginPath();
        ctx.moveTo(forceArrowStartX, forceArrowY);
        ctx.lineTo(forceArrowEndX, forceArrowY);
        ctx.stroke();
        drawArrowhead(ctx, forceArrowEndX, forceArrowY, 12);
        ctx.font = 'italic 30px Georgia';
        ctx.fillText('F', forceArrowStartX - 20, forceArrowY);

        // 9. Figure Label
        ctx.font = '22px Georgia';
        ctx.fillText('Fig. 1.6.', canvasWidth / 2, groundY + 45);

    </script>
</body>
</html>